﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Security.Permissions;

using NPMonitor.Helpers;
using NPMonitor.Configuration;
using NPMonitor.DataModel;

namespace NPMonitor.Implementation
{
    [Serializable]
    public class DownloadSettings
    {
        #region 对象属性

        public List<DownloadObject> Objects { get; set; }

        public string Folder { get; protected set; }

        #endregion

        #region 构造函数

        internal DownloadSettings()
            : this(@"Default")
        {
        }

        public DownloadSettings(string folder)
        {
            Objects = new List<DownloadObject>();

            this.Folder = folder;
        }

        #endregion

        public DownloadObject AddDownload(string url)
        {
            DownloadObject download = new DownloadObject(url);
            this.Objects.Add(download);

            return download;
        }

        public DownloadObject AddDownload(string url, string saveName)
        {
            DownloadObject download = new DownloadObject(url, saveName);

            this.Objects.Add(download);

            return download;
        }

        public string Serialize()
        {
            return Utils.SerializeObject<DownloadSettings>(this);
        }

        public string GetStoreFolder()
        {
            string folderPath = this.Folder;

            if (!IsAbsolutePath(folderPath))
            {
                folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, folderPath);
            }

            return folderPath;
        }

        public string GetStoreFilePath(DownloadObject dobj)
        {
            string folderPath = GetStoreFolder();

            string storePath = Path.Combine(folderPath, dobj.SaveName);

            return storePath;
        }

        #region Private Methods

        /// <summary>
        /// return true if path string contain char ':', return false if not 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private bool IsAbsolutePath(string path)
        {
            if (path.IndexOf(':') >= 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion
    }
}
